<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Model\Admin\Config;
use Illuminate\Support\Facades\Input;

class ConfigController extends CommonController
{
  //显示配置管理页 admin/Config
	public function index(){
		$data = Config::orderBy('conf_order','asc')->get();
    foreach ($data as $key => $value) {
      if($value->field_type == 'input'){
        $str = '<input type="text" name="conf_content[]" class="lg" value="'.$value->conf_content.'">';
        $data[$key]->_html = $str;
      }else if($value->field_type == 'textarea'){
        $str = '<textarea name="conf_content[]" class="lg">'.$value->conf_content.'</textarea>';
        $data[$key]->_html = $str;
      }else{
        $arr = explode(',', $value->field_value);
        $str = "";
        foreach ($arr as $m => $n) {
          $arr2 = explode('|', $n);
          $checked = $value->conf_content == $arr2[0] ? 'checked' : '';
          $str .='<input type="radio" name="conf_content[]" value="'.$arr2[0].'"'.$checked.'>'.$arr2[1].'　';
        }
        $data[$key]->_html = $str;
      }
    }
		return view('admin.config.index')->with('data', $data);
	}

  //
  public function changeContent(){
    $input = Input::all();
    foreach ($input['conf_id'] as $key => $value) {
      Config::where('conf_id',$value)->update(['conf_content'=>$input['conf_content'][$key]]);
    }
    $this->putFile();
    return back()->with('success','配置修改成功！');
  }

  // 将配置文件写进 /config/web.php
  public function putFile(){
    // 过滤数据，取 conf_name=>conf_content  all()是为了去掉附带信息
    $config = Config::pluck('conf_content','conf_name')->all();
    // 拼接成字符串 
    $data = '<?php  return '.var_export($config,true).';';
    // 合成文件路径
    $path = base_path().'\config\web.php';
    // 将文件写进去
    file_put_contents($path, $data);
  }

	// 更新排序
  public function changeOrder(){
  	$input = Input::all();
  	$config = Config::find($input['conf_id']);
  	$config->conf_order = $input['conf_order'];
  	$result = $config->update();
  	if($result){
  		$data = [
  			'status' => '1',
  			'msg'  => '排序更新成功',
  		];
  	} else {
  		 $data = [
  		 	'status' => '0',
  		 	'msg' => '排序更新失败',
  		 ];
  	}
  	return $data;
  }

  // 添加界面显示  get. admin/Config/create
  public function create(){
  	return view('config.add');
  }

 	// post. admin/Config 添加数据处理
 	public function store(){
    $input = Input::except('_token');
    $result = Config::create($input);
    if($result){
      $this->putFile();
      return redirect('admin/config')->with('success','配置添加成功！');
    }else{
      return back()->with('errors','添加失败！未知错误！');
    }
 	}

  // 修改页面显示 get. admin/config/{config}/edit
  public function edit($conf_id){
    $data = Config::find($conf_id);
    return view('admin.config.edit',compact('data'));

  }

  // 修改界面数据处理 put admin/Config/{Config}
  public function update($conf_id){
    $input = Input::except('_token','_method');
    $result = Config::where('conf_id',$conf_id)->update($input);
    if($result !== false){
      return redirect('admin/config')->with('success','修改成功！');
    }else{
      return back()->with('errors','修改失败！');
    }
  }

  public function destroy($conf_id){
    $result = Config::where('conf_id',$conf_id)->delete();
    if($result){
      $this->putFile();
      $data = [
        'status'    =>    '1',
        'msg'       =>    '删除成功！',
      ];
    }else{
      $data = [
        'status'    =>    '0',
        'msg'       =>    '删除失败！',
      ];
    }
    return $data;
  }

  //界面显示
	public function show(){

	}
}
